//
//  LWPhotoStatusView.m
//  1.28  weibo
//
//  Created by mac on 15/2/24.
//  Copyright (c) 2015年 mac. All rights reserved.

#import "LWPhotoStatusView.h"
#import "LWstatuses.h"
#import "LWPhotoCollectionViewCell.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "LWPhotos.h"

@interface LWPhotoStatusView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation LWPhotoStatusView

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.status.pic_urls.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LWPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    LWPhotos *photos = nil;
    photos = self.status.pic_urls[indexPath.item];
    cell.photos = photos;
    return cell;
}

#pragma mark - UICollectionViewDelegate的代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建浏览器
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    
    // 2.告诉浏览器需要显示"哪些"内容
    NSMutableArray *photos = [NSMutableArray array];
    for (int i = 0; i < self.status.pic_urls.count; i++) {
        // 1.创建数组需要的模型对象
        MJPhoto *photo = [[MJPhoto alloc] init];
        // 2.告诉模型对象需要显示的图片地址
        LWPhotos *photoModel = self.status.pic_urls[i];
        photo.url = [NSURL URLWithString:photoModel.bmiddle_pic];
        
        // 3.告诉模型对象显示的图片对应的原始图片是谁
        // 3.1取出对应cell
        NSIndexPath *path = [NSIndexPath indexPathForItem:i inSection:0];
        LWPhotoCollectionViewCell *cell = (LWPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:path];
        photo.srcImageView = cell.photo;
        
        [photos addObject:photo];
    }
    
    browser.photos = photos;
    // 3.告诉浏览器当前需要显示的索引
    browser.currentPhotoIndex = indexPath.item;
    // 4.显示浏览器
    [browser show];
}

@end
